/**
 * 前台教师PC端
 * 		学生刷卡记录JS代码
 */

var pageNo = 1;     //当前页
var pageSize = 8;   //每页记录数
var pageCount;
var tabCurrent = 0; //当前标签，进校，出校，异常记录
var islogin = 1;
var dataJson;       //查询参数
//jQuery(function ($) {
$(function () {
    /*--------------------------------------------------------------------*/
	$(".exit-btn").click(function () {
        $.ajax({
            url: "/huaqiao/front/teacher/exit?" + new Date().getTime(),
            async: false,
            success: function (){
                window.location.href = "/huaqiao/front_end/pc/teacher/login_pc_teacher.html";
            }
        });
    });
    $(".search-btn").click(function () {
        pageNo = 1;
        queryData();
        return false;
    });
    laydate({
        elem: '#date-start',
        format: 'YYYY-MM-DD hh:mm:ss',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            pageNo = 1;
            queryData();
        }
    });
    laydate({
        elem: '#date-end',
        format: 'YYYY-MM-DD hh:mm:ss',
        istime: false,
        isclear: false, //是否显示清空
        istoday: false, //是否显示今天
        festival: true, //显示节日
        choose: function (datas) { //选择日期完毕的回调
            pageNo = 1;
            queryData();
        }
    });
	init();
});

/*-------------------------------设置分页事件开始-------------------------------------*/

/*-------------------------------设置分页事件结束-------------------------------------*/


function init() {
    $.ajax({
//      url: "/huaqiao/front/teacher/getCurTea?" + new Date().getTime(),
		url:"http://localhost:8080/hq/teacher/info.do",
        async: false,
        success: function (str) {
            if (check(str)) {
                $("#teacher_name").text(str.teachername);
                $("#teacher_class").text(str.teacherclass);
            }
        }
    });
//  queryData();
}

/**
 * 检查用户是否登录
 * 	如果未登录，返回教师端登录页面
 * @param str
 * @returns {Boolean}
 */
function check(str){
	if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage,{time:1000});
                    window.location.href = "http://localhost:8080/hq/front_end/pc/teacher/login_pc_teacher.html";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}


function queryData() {
    tabCurrent = $("#type").attr("value");
	var sdate = $("#date-start").val();
    var edate = $("#date-end").val();
    var stuName = $(".seek-box").val();
    
    if (sdate == "") {
        sdate = "1970-01-01 00:00:00";
    }
    if (edate == "") {
        edate = "2160-01-01 00:00:00";
    }
    dataJson = {
    	startDate: sdate,
        endDate: edate,
        stuName: stuName,
        pageNo : pageNo,
        pageSize : pageSize,
        type: tabCurrent
    };
    getDataOfRecord();
}

function getDataOfRecord() {//带参分页查询函数
    $.ajax({
        async:true,
        type:'POST',
        url:'http://localhost:8080/hq/teacher/query.do',
        data:dataJson,
        cache:false,
        dataType : 'json',
        contentType:'application/x-www-form-urlencoded',
        beforeSend:function (xhr) { },
        success:function (data,textStatus,xhr) {
        	if(check(data)){
                if(data.itemCount==0){
                    layer.msg("没有数据",{time:1000});
                }
                pageCount=Math.floor((data.itemCount + pageSize - 1) / pageSize);
                setTableBTN();
            	setDataOfList(data.list);
        	}
        },
        error:function (xhr,textStatus,errorInfo) { },
        complete:function (xhr,textStatus) {}
    });
}

function setDataOfList(records) {
    $(".go-out").html("");
    var str = '';
    for(var i=0;i<records.length;i++){
        str += "<tr>";
        str += "<td>" + records[i].studentName + "</td>";
        if(tabCurrent==3){
        	var typeIO = records[i].inoutType==0?'进校':'出校';
        	str += "<td>" + typeIO + "</td>"; 
        }
        str += "<td>" + DateUtils.format(new Date(records[i].createTime),"yyyy-MM-dd HH:mm:ss") + "</td>";
        str += "</tr>";
    }
    // $("#pageCount").text(pager.pageCount);
    $(".go-out").html(str);
}

function setTableBTN() {
    var pagination_li = $(".pager li");
    var pagination_a = $(".pager a");
    
    pagination_li.each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
                if (pageNo > 1) {
                    pageNo--;

                    if (pageNo < parseInt(pagination_a.eq(2).html())) {
                        pagination_a.eq(6).html(pageNo + 4);
                        pagination_a.eq(5).html(pageNo + 3);
                        pagination_a.eq(4).html(pageNo + 2);
                        pagination_a.eq(3).html(pageNo + 1);
                        pagination_a.eq(2).html(pageNo);
                    }
                    pagination_li.each(function (i) {
                        $(this).removeClass("active");
                        if (pageNo == parseInt($(this).text())) {
                            $(this).addClass("active");
                        }
                    });
                    queryData();
                } else {
                    layer.msg("当前是第一页",{time:1000});
                }
            };
        } else if (i == 7) {//下一页按钮
            this.onclick = function () {
                if (pageNo < pageCount) {
                    pageNo++;

                    if (pageNo > parseInt(pagination_a.eq(6).html())) {
                        pagination_a.eq(6).html(pageNo);
                        pagination_a.eq(5).html(pageNo - 1);
                        pagination_a.eq(4).html(pageNo - 2);
                        pagination_a.eq(3).html(pageNo - 3);
                        pagination_a.eq(2).html(pageNo - 4);
                    }
                    pagination_li.each(function (i) {
                        $(this).removeClass("active");
                        if (pageNo == parseInt($(this).text())) {
                            $(this).addClass("active");
                        }
                    });
                    queryData();
                } else {
                    layer.msg("当前是最后一页",{time:1000});
                }
            };
        } else if (i == 0) {
            this.onclick = function () {
                pageNo = 1;

                if (pageNo < parseInt(pagination_a.eq(2).html())) {
                    pagination_a.eq(6).html(pageNo + 4);
                    pagination_a.eq(5).html(pageNo + 3);
                    pagination_a.eq(4).html(pageNo + 2);
                    pagination_a.eq(3).html(pageNo + 1);
                    pagination_a.eq(2).html(pageNo);
                }
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageNo == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                queryData();
            };
        } else if (i == 8) {
            this.onclick = function () {
                pageNo = pageCount;

                if (pageNo > parseInt(pagination_a.eq(6).html())) {
                    pagination_a.eq(6).html(pageNo);
                    pagination_a.eq(5).html(pageNo - 1);
                    pagination_a.eq(4).html(pageNo - 2);
                    pagination_a.eq(3).html(pageNo - 3);
                    pagination_a.eq(2).html(pageNo - 4);
                }
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageNo == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                queryData();
            };
        } else {//普通翻页按钮
            this.onclick = function () {
                var index = parseInt(this.innerText);
                if (index > pageCount) {
                    index = pageCount;
                    layer.msg("共"+pageCount+"页",{time:1000});
                }
                pageNo = index;
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageNo == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                queryData();
            }
        }
    });
}